package com.tom.girl.Controller;
import com.tom.girl.GirlRepository;
import com.tom.girl.model.Girl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController

public class GirlController {
    @Autowired
    private GirlRepository girlRepository;
    /*
    * Select girls
    * */
    @GetMapping(value = "/selectall")
    public List<Girl>  girlList(){
       return girlRepository.findAll();
    }

    /*
    * insert girl
    * */
    @PostMapping(value = "/add")
    public Girl girlAdd(@RequestParam("name") String name,
                          @RequestParam("id") Integer id,
                          @RequestParam("age") Integer age){
        Girl girl = new Girl();
        girl.setId(id);
        girl.setAge(age);
        girl.setName(name);

        return  girlRepository.save(girl);
    }

    @GetMapping(value = "/selectone")
    public Girl selectOne(@RequestParam("id") Integer id){
        return  girlRepository.findOne(id);
    }
    /*
    * delet one
    * */
    @DeleteMapping(value = "/del")
    public void deOne(@RequestParam("id") Integer id){
        girlRepository.delete(id);
    }
    /*
    * update one
    * */
    @PutMapping(value = "/upd")
    public void updOne(@RequestParam("id") Integer id,
                        @RequestParam("name") String name,
                       @RequestParam("age") Integer age){
                Girl girl = new Girl();
                girl.setId(id);
                girl.setAge(age);
                girl.setName(name);
                girlRepository.save(girl);
        }
/*
* findbyAge
* */
@GetMapping("findByAge")
    public List<Girl> findByAge(@RequestParam("age") Integer age){
        return girlRepository.findByAge(age);
}
}
